package com.pocketsweet.ui.uilib;

import java.util.ArrayList;
import java.util.List;

import com.pocketsweet.R;
import com.pocketsweet.ui.uilib.SegmentView.onSegmentViewClickListener;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SimpleViewPagerIndicator extends LinearLayout {

	private static final int COLOR_TEXT_NORMAL = 0xFF000000;
	private static final int COLOR_INDICATOR_COLOR = Color.GREEN;
	private onTextClickListener listener;

	private String[] mTitles;
	private int mTabCount;
	private int mIndicatorColor = COLOR_INDICATOR_COLOR;
	private float mTranslationX;
	private Paint mPaint = new Paint();
	// tap 宽度
	private int mTabWidth;
	private int tabwidth;
	private int oneOffset;
	// tab颜色
	private int tapColor;
	// 文字大小
	private int textSize;
	// 文字颜色
	private int textColor;
	private int textSelelctColor;
	// deliver 颜色
	private int deliverColor;
	private List<TextView> textList;

	public SimpleViewPagerIndicator(Context context) {
		this(context, null);
		textList = new ArrayList<TextView>();
	}

	public SimpleViewPagerIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		textList = new ArrayList<TextView>();

		TypedArray array = context.obtainStyledAttributes(attrs,
				R.styleable.SlideTap);
		tabwidth = array.getDimensionPixelOffset(R.styleable.SlideTap_TapSize,
				0);
		tapColor = getResources().getColor(
				array.getResourceId(R.styleable.SlideTap_TapColor, 0));
		textColor = getResources().getColor(
				array.getResourceId(R.styleable.SlideTap_TextColor, 0));
		textSelelctColor = getResources().getColor(
				array.getResourceId(R.styleable.SlideTap_TextSelelctColor, 0));
		// 设置字体
		textSize = array.getInt(R.styleable.SlideTap_TextSize, 16);
		deliverColor = getResources().getColor(
				array.getResourceId(R.styleable.SlideTap_deliverColor, 0));
		mPaint.setColor(tapColor);
		mPaint.setStrokeWidth(9.0F);
		array.recycle();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mTabWidth = w / mTabCount;
		oneOffset = (mTabWidth - tabwidth) / 2;
	}

	public void setTitles(String[] titles) {
		mTitles = titles;
		mTabCount = titles.length;
		generateTitleView();

	}

	public void setIndicatorColor(int indicatorColor) {
		this.mIndicatorColor = indicatorColor;
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		canvas.save();
		canvas.translate(mTranslationX, getHeight() - 2);
		canvas.drawLine(oneOffset, 0, mTabWidth - oneOffset, 0, mPaint);
		canvas.restore();
	}

	public void scroll(int position) {
		/**
		 * <pre>
		 *  0-1:position=0 ;1-0:postion=0;
		 * </pre>
		 */
		for (int i = 0; i < textList.size(); i++) {
			if (i == position) {
				textList.get(i).setTextColor(textSelelctColor);
			} else {
				textList.get(i).setTextColor(textColor);
			}
		}
		mTranslationX = (getWidth() / mTabCount) * (position);
		invalidate();

	}

	public void setOffset(int position, float offset) {

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return super.dispatchTouchEvent(ev);
	}

	private void generateTitleView() {
		if (getChildCount() > 0)
			this.removeAllViews();
		int count = mTitles.length;

		setWeightSum(count);
		for (int i = 0; i < count; i++) {
			TextView tv = new TextView(getContext());
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
					LayoutParams.MATCH_PARENT);
			lp.weight = 1;
			tv.setGravity(Gravity.CENTER);
			if (i == 0) {
				tv.setTextColor(textSelelctColor);
			} else {
				tv.setTextColor(textColor);
			}
			tv.setText(mTitles[i]);
			tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
			tv.setLayoutParams(lp);
			final int position = i;
			tv.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					listener.onTextClick(v, position);
				}
			});
			addView(tv);
			textList.add(tv);
			if (i != count) {
				LinearLayout line = new LinearLayout(getContext());
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
						1, LayoutParams.MATCH_PARENT);
				line.setBackgroundColor(deliverColor);
				line.setLayoutParams(params);
				addView(line);
			}
		}
	}

	public void setOnTextClickListener(onTextClickListener listener) {
		this.listener = listener;
	}

	public static interface onTextClickListener {
		public void onTextClick(View v, int position);
	}

}
